package com.daocao.yfb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.daocao.yfb.R;
import com.daocao.yfb.adapter.MsgTipsAdapter;
import com.daocao.yfb.api.Api;
import com.daocao.yfb.api.bean.NoticeIndexBean;
import com.daocao.yfb.api.model.NoticeIndexModel;
import com.daocao.yfb.base.BaseActivity;
import com.daocao.yfb.util.Network;
import com.daocao.yfb.util.RxLifeUtil;
import com.daocao.yfb.util.ToastUtil;
import com.daocao.yfb.util.UI;
import com.daocao.yfb.view.V19FrameLayout;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MsgTipsActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_order_msg)
    ImageView imgOrderMsg;
    @BindView(R.id.txt_order_msg)
    TextView txtOrderMsg;
    @BindView(R.id.img_system_msg)
    ImageView imgSystemMsg;
    @BindView(R.id.txt_system_msg)
    TextView txtSystemMsg;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private MsgTipsAdapter adapter;
    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_tips);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    public void getNoticeIndex() {

        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();

        Api.api_service.getNoticeIndex().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });

    }

    private void setData(NoticeIndexModel.DatasBean datas) {
        NoticeIndexModel.DatasBean.MsgBean msg = datas.msg;
        if (msg.order_msg == 0) {
            txtOrderMsg.setVisibility(View.GONE);
        } else {
            txtOrderMsg.setVisibility(View.VISIBLE);
            txtOrderMsg.setText(msg.order_msg+"");
        }
        if (msg.system_msg == 0) {
            txtSystemMsg.setVisibility(View.GONE);
        } else {
            txtSystemMsg.setVisibility(View.VISIBLE);
            txtSystemMsg.setText(msg.order_msg+"");
        }

        adapter.update(datas.list);
    }

    private void init() {
        txtTitle.setText("消息提醒");
        adapter = new MsgTipsAdapter(this);
        list.setAdapter(adapter);

        getNoticeIndex();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.img_order_msg, R.id.img_system_msg})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.img_order_msg:
                Intent intent = new Intent(this, MsgItemTipsActivity.class);
                intent.putExtra("type", "1");
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case R.id.img_system_msg:
                Intent intentSys = new Intent(this, MsgItemTipsActivity.class);
                intentSys.putExtra("type", "2");
                startActivity(intentSys);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }
}
